mui.init();


/**
 * 当从登录页面返回时，回调用当前“刷新”事件 
 */
window.addEventListener('reload',function(event){
	reload();
});

function gradechange(){
	var obJS = document.getElementById("bankNo");
	var value = obJS.options[obJS.selectedIndex].value;
	if(value == 102){
	 $("#banklimit").text("充值限额：单笔5千/单日5万/单月5万");
	}
	if(value == 105){
	 $("#banklimit").text("充值限额：单笔1万/单日1万/单月5万");
	}
	if(value == 103){
	 $("#banklimit").text("充值限额：单笔5千/单日5千/单月不限");
	}
	if(value == 403){
	 $("#banklimit").text("充值限额：单笔5千/单日5千/单月不限");
	}
	if(value == 104){
	 $("#banklimit").text("充值限额：单笔1万/单日1万/单月不限");
	}
	if(value == 308){
	 $("#banklimit").text("充值限额：单笔2万/单日2万/单月不限");
	}
	if(value == 303){
	 $("#banklimit").text("充值限额：单笔5万/单日5万/单月不限");
	}
	if(value == 306){
	 $("#banklimit").text("充值限额：单笔2万/单日2万/单月不限");
	}
	if(value == 302){
	 $("#banklimit").text("充值限额：单笔5万/单日5万/单月20万");
	}
	if(value == 309){
	 $("#banklimit").text("充值限额：单笔5百/单日5百/单月5百");
	}
	if(value == 106){
	 $("#banklimit").text("充值限额：单笔5万/单日5万/单月不限");
	}
}

function reload(){
	resetForm();
	//loadData();
}

mui(".question-select").on("tap", "div", function() {
	var self = this;
	$("#TypeSelected").removeAttr("id").removeClass("activity");
	$(self).attr("id", "TypeSelected").addClass("activity");
});

function resetForm(){
	//sp_bankName,sp_bankAccount,specBankCardNo
	FormMgr.resetFields(["bankNo","specBankCardNo","money"],true); /*清空密码字段*/
	document.getElementById("bankNo").selectedIndex = 0;
	$("[name='checkbox']").removeAttr("checked");//取消全选
}

function loadData(){
	var url = UrlConfig.mrecharge_getInfo;
	MuiEventMgr.ajax(url,{
	  	pars:{},
	  	sfn : function(data){
	  		var success = data.success;
			if(!success){
				var status = data.status;
				var msg = data.msg;
				var _msg = "充值之前请先绑定银行卡，确定现在绑定银行卡";//msg.msg;
				var status = msg.status;
				var btn = ["确定","取消"];
				mui.confirm(_msg,'提示',btn,function(e){
					if(e.index==0){
						forward_bankPage(status);
					}
				});
			}else{
				renderDatas(data);
			}
	 	},ffn : function(){
	 		mui.alert("获取充值信息失败!");
	 	}
	});
}


function renderDatas(data){
 	data = data.msg;
	var appId = data.appId;
	var bankName = data.bankName;
	var bankAccount = data.bankAccount;
	var appId = data.appId;
	$("#sp_bankName").text(bankName);
	$("#sp_bankAccount").text(bankAccount);
	$("#bankNo").val(appId);
	$("#specBankCardNo").val(bankAccount);
}


function forward_bankPage(){
	var url = "../mybank/mybank.html";
	var targetPageId = plus.webview.currentWebview().id;
	plus.storage.setItem(KeyCfg.targetPageId_anquanyz,targetPageId);
	var cfg = {pars:{}};
	MuiEventMgr.checkSecuityForwad(function(){
		openPage(url,{});
	});
			
}

/**
 * 打开新页面 
 * @param {Object} url	新地址
 * @param {Object} pars	参数
 */
function openPage(url ,pars){
	if(!pars) pars = {};
	var cfg = {pars : pars};
	MuiEventMgr.openBySafe(url,cfg);
}

mui.plusReady(function() {
	(function(){
		resetForm();
		//loadData();
	})();
	
	(function(){/*初始化事件*/
		var a_changeCard = CmwMcore.$('a_changeCard');
		MeventMgr.onTouchstart(a_changeCard,function(event,self){
			forward_bankPage();
		});
		
		
		//下一步事件
		var btn_login = CmwMcore.$('btn_login');
		MeventMgr.onTouchstart(btn_login,function(event,self){
			var dis=$("#btn_login").attr("disabled")//判断是否有这属性
			if(dis)return; 
			if(!valid()) return false;
			var money = $("#money").val();
			var url = UrlConfig.mrecharge_vailIDCard;
			MuiEventMgr.ajax(url,{
			  	pars:{param:money},
			  	sfn : function(data){
			  		var success = data.success;
			  		console.log("success="+success); 
			  		var msg = data.msg;
			  		if(success){
			  			submitForm();
			  		}else{
			  			mui.alert("尊敬的客户，经过身份验证系统检测，您的姓名和身份证号码不匹配，导致无法正常充值，详情请致电来理财客服热线 4008122468，谢谢。");
			  		}
			  	},ffn : function(data){
			  		mui.alert("尊敬的客户，经过身份验证系统检测，您的姓名和身份证号码不匹配，导致无法正常充值，详情请致电来理财客服热线 4008122468，谢谢。");
			  	}
			 });
		});
	})();
 
//	跳转协议页面
	var a_mgr = CmwMcore.$("a_mgr");
	MeventMgr.onTouchstart(a_mgr,function(event,self){
	var cfg = {pars :{}};
	MuiEventMgr.open('../mgrPro/mgrProtocol1.html',cfg);
	return false;
	});
	
	/*表单验证*/
	function valid(){		
		
		var bankNo_val = $('#bankNo').val();
		if(!bankNo_val){
			mui.alert("请选择所属银行!");
			return false;
		}
		
		var specBankCardNo_val = $('#specBankCardNo').val();
		if(!specBankCardNo_val){
			mui.alert("请输入银行卡号!");
			return false;
		}
		var bankCardNumber = specBankCardNo_val.replace(/\s+/g,"");
		if(bankCardNumber.trim().length != 16 && bankCardNumber.trim().length != 24 && bankCardNumber.trim().length != 19){
			mui.alert("卡号长度不正确!");
			return false;
		}
			
		var money = $("#money").val();
		if(!money || parseFloat(money) <= 0){
			mui.alert("充值金额不得低于100元!");
			return false;
		}
		if(!$("input[name='agree']").is(":checked")){
			mui.alert("您还没有同意《来理财资金管理规定》!");
			return false;
		}
		return true;
	}
	
	function submitForm(){
		CmwMcore.disabled('btn_login');
		var formDatas = $("#theForm").serialize();
		var url = UrlConfig.mrecharge_gotoPage;
	
		MuiEventMgr.ajax(url,{
		  	pars:formDatas,
		  	sfn : function(data){
		  		console.log("sfn.success="+data.success);
		  		callback(data); 
	        		CmwMcore.enabled('btn_login');
		  	},ffn : function(data){
		  			console.log("ffn.success="+data.success);
		  		CmwMcore.enabled('btn_login');
		  	}
		 });
	}
	
	function callback(data){
		var success = data.success;
		var msg = data.msg;
		if(success){
			var bankAccount = $("#specBankCardNo").val().trim();
			var bankNo = $("#bankNo").val();
			var bankName = $("#bankNo").find("option:selected").text();
			var tradeAmount = msg.tradeAmount;
			var tradeNum = msg.tradeNum;
			var tradeInfo = msg.tradeInfo;
			var pars = {bankName:bankName,bankAccount:bankAccount,bankNo:bankNo,tradeAmount:tradeAmount,tradeNum:tradeNum,tradeInfo:tradeInfo};
			var url = "recharge_confirm.html";
			openPage(url,pars);
		}else{
		  mui.alert("充值失败");
		}
	}
	
		/**
	 * 打开新页面 
 	 * @param {Object} url	新地址
	 * @param {Object} pars	参数
	 */
	function openPage(url ,pars){
		if(!pars) pars = {};
		var cfg = {pars : pars};
		MuiEventMgr.openBySafe(url,cfg);
	}
});